<template>
  <el-dialog
    :title="title"
    :visible.sync="isOpen"
    append-to-body
    @close="handleClose"
    width="75%"
  >
    <div>
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="姓名" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="输入姓名"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input
            v-model="queryParams.phone"
            placeholder="输入手机号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="类型" prop="personnelTypeDict">
          <el-select
            v-model="queryParams.personnelTypeDict"
            placeholder="选择类型"
            clearable
            @change="handleQuery"
          >
            <el-option
              v-for="dict in dict.type.biz_personnel_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-table
        v-loading="loading"
        :data="tableList"
        :highlight-current-row="!multiple"
        @current-change="handleCurrentChange"
        @selection-change="handleSelectionChange"
        :row-key="getRowKeys"
        stripe
        ref="table"
      >
        <el-table-column
          v-if="multiple"
          :reserve-selection="true"
          type="selection"
          width="50"
          align="center"
        />
        <el-table-column label="序号" type="index" align="center">
          <template slot-scope="scope">
            <span>{{
              (queryParams.pageNum - 1) * queryParams.pageSize +
              scope.$index +
              1
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="部门"
          align="center"
          min-width="120"
          key="deptName"
          prop="deptName"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="姓名"
          align="center"
          min-width="120"
          key="name"
          prop="name"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <el-button type="text" @click="handleDetail(scope.row)">{{
              scope.row.name
            }}</el-button>
          </template>
        </el-table-column>
        <!-- <el-table-column label="性别" align="center" key="sexDict" prop="sexDict" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sexDict" />
          </template>
        </el-table-column> -->
        <el-table-column
          label="类型"
          align="center"
          min-width="100"
          key="personnelTypeDict"
          prop="personnelTypeDict"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.biz_personnel_type"
              :value="scope.row.personnelTypeDict"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="电话"
          align="center"
          min-width="120"
          key="phone"
          prop="phone"
          :show-overflow-tooltip="true"
        />
      </el-table>
      <!-- <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="handlePageChange"
      /> -->
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleConfirm">确 定</el-button>
      <el-button @click="handleClose">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { list } from "@/api/organization/civil-servant";

export default {
  name: "dept_check-civil-servant",
  dicts: [
    "biz_personnel_type",
    "sys_user_sex",
    "biz_civil_servant_post",
    "biz_civil_servant_special_situation",
  ],
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    // queryParams: {
    //   type: Object,
    //   default: () => {}
    // },
    multiple: {
      type: Boolean,
      default: false,
    },
    multipleList: {
      type: Array,
      default: [],
    },
  },
  components: {},
  data() {
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        phone: undefined,
        personnelTypeDict: undefined,
        postDict: undefined,
      },
      selections: "",
      title: "部门成员",
      ids: "",
      isOpen: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      tableList: [],
      loading: false,
    };
  },
  computed: {},
  watch: {
    visible: {
      async handler(val) {
        this.isOpen = val;
        await this.getList();
        this.$nextTick(() => {
          if (this.multipleList.length) {
            this.selections = [];
            this.$refs.table.clearSelection();
            for (let id of this.multipleList) {
              let row = this.tableList.find((item) => item.userId == id);
              if (row) {
                this.selections.push(row);
                this.$refs.table.toggleRowSelection(row, true);
              }
            }
          }
        });
      },
    },
  },
  methods: {
    // 表格分页切换
    handlePageChange(a,b,c){
      console.log(a,b,c);
    },
    getRowKeys(row) {
      return row.id
    },
    handleConfirm() {
      this.$emit("success", this.selections);
      this.handleClose();
    },
    async getList() {
      this.loading = true;
      await list({...this.queryParams,pageSize:1000}).then((response) => {
        this.tableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 单选选中数据
    handleCurrentChange(currentRow, oldCurrentRow) {
      this.selections = currentRow;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selections = selection;
      this.ids = selection.map((item) => item.id);
    },
    // 关闭弹框
    handleClose() {
      this.resetQuery();
      this.$emit("update:visible", false);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.$refs.table.clearSort();
      if (this.multiple) {
        this.$refs.table.clearSelection();
      } else {
        this.$refs.table.setCurrentRow();
      }
      this.handleQuery();
    },
  },
};
</script>

<style lang="scss"></style>
